stopcam = false;
_Offset = [0,15,0];
Zpos = 0;
VtsHight = 0.0;
VTS_Freecam_nvg = false;
VTS_Freecam_on = true;
VTS_Freecam_help = false;
VTS_Freecam_players = [];
_playableUnits = playableUnits;
VTS_Freecam_target = player;
//rabbit ! because we can attach stuff to it !
Obj = "Rabbit" createVehiclelocal [spawn_x , spawn_y ,spawn_z] ;
Obj hideObject true; 
[Obj] execVM "scripts\VTS_Functions\dontfall.sqf";
lapin = Obj;
	

object3D = console_unit_unite createVehicleLocal [spawn_x, spawn_y ,spawn_z] ;
object3D allowDamage false;
if (nom_console_valid_type == "Man")  then {VtsHight = 0;_Offset = [0,5.0,0];};
if (nom_console_valid_type in type_objects) then {VtsHight = -0.5;_Offset = [0,5.0,0]; };
if (nom_console_valid_type == "Empty")  then {VtsHight = 0;_Offset = [0,15.0,0];};	 
object3D attachTo [Obj,[0,0,VtsHight]]; 

ObjArrow = "Sign_arrow_down_large_EP1" createVehicleLocal [spawn_x, spawn_y ,spawn_z] ;
ObjArrow attachTo [Obj,[0,0,0]]; 

			
OBJdir = 0;

Obj allowDamage false;

sleep 0.25;
//create camera
VTS_Freecam = "camconstruct" camcreate [spawn_x, spawn_y, 7];
VTS_Freecam camConstuctionSetParams [[spawn_x,spawn_y,spawn_z], 20000, 10000];
VTS_Freecam camSetTarget player;
VTS_Freecam cameraeffect ["internal","back"];

//Spacekey to exit
_keydown_space = (finddisplay 46) displayaddeventhandler ["keydown", "
	if ((_this select 1) == 57 ) then {
		stopcam = true;
	};
"];

_keydown_Backspace = (finddisplay 46) displayaddeventhandler ["keydown", "
	if ((_this select 1) == 14 ) then {
		stopcam = true;
	};
"];

//Spawn the unit 

_keydown_c_key = (finddisplay 46) displayaddeventhandler ["keydown", "
  if ((_this select 1) == 46 )then  {
    if (placingdone < 1) then { placingdone=1; _tmp= [] execVM""Computer\spectator\place.sqf"";};
  };
  "];

/*
_keydown_ShiftL = (finddisplay 46) displayaddeventhandler ["keydown", "
	if ((_this select 1) == 42 ) then {
		ShiftL = true;
		}
		else {
		ShiftL = false;
	};
"];
*/

_keydown_NightVision = (finddisplay 46) displayaddeventhandler ["keydown", "
	if ((_this select 1) in (actionkeys ""NightVision"")) then {
		if (VTS_Freecam_nvg) then {camUseNVG false;VTS_Freecam_nvg = false;}
		else {camUseNVG true;VTS_Freecam_nvg = true;};
	};
"];


_keydown_Help = (finddisplay 46) displayaddeventhandler ["keydown", "
	if ((_this select 1) in (actionkeys ""Help"")) then {
		if (not VTS_Freecam_help) then {
			Titletext  [""
                    Mousewheel:                                       Change height\n
                    Left mouse button:                                  Rotate left\n
                    Right mouse button:                                Rotate right\n
                    C:                                                 Place object\n
                    Space:                                                     Exit\n
                    Movement keys and mouse:                            Move camera\n
                                                                                   \n
                    Q:                                               Move camera up\n
                    Z:                                             Move camera down\n
                    SHIFT Left:                                       Slow movement\n
                                                                                   \n
                    Nightvision key:                             Nightvision On/Off\n
                                                                                   \n
                    Press the help button to exit the help screen."",""PLAIN""];
			VTS_Freecam_help = true;
		}
		else {
			Titletext ["""",""PLAIN""];
			VTS_Freecam_help = false;
		};
	};
"];

_keydown_Rclick = (finddisplay 46) displayaddeventhandler ["MouseButtonDown", "
	if ((_this select 1) == 0) then {
	OBJdir = getdir Obj;
	Obj setDir (OBJdir - 22.5);
};
"];


_keydown_Lclick = (finddisplay 46) displayaddeventhandler ["MouseButtonDown", "
	if ((_this select 1) == 1) then {
	OBJdir = getdir Obj;
	Obj setDir (OBJdir + 22.5);
};
"];

_mousez = (findDisplay 46) displayAddEventHandler ["mousezchanged", "
		_z = _this select 1;
		if (_z  < 0) then {
			Zpos = Zpos - 0.3;
		}
		else {
			Zpos = Zpos + 0.3;
		};
"];


/*
_mousez = (findDisplay 46) displayAddEventHandler ["mousezchanged", "
	if ({alive _x} count VTS_Freecam_players > 0) then {
		_z = _this select 1;
		if (_z  < 0) then {
			hint "up";
			VTS_Freecam_players_select = VTS_Freecam_players_select - 1;
			if (VTS_Freecam_players_select < 0) then {VTS_Freecam_players_select = VTS_Freecam_players_count};
			while {not (alive (VTS_Freecam_players select VTS_Freecam_players_select))} do {
				VTS_Freecam_players_select = VTS_Freecam_players_select - 1;
				if (VTS_Freecam_players_select < 0) then {VTS_Freecam_players_select = VTS_Freecam_players_count};
			};
			VTS_Freecam_target = vehicle (VTS_Freecam_players select VTS_Freecam_players_select);		
		}
		else {
			hint "down";
			VTS_Freecam_players_select = VTS_Freecam_players_select + 1;
			if (VTS_Freecam_players_select > VTS_Freecam_players_count) then {VTS_Freecam_players_select = 0};
			while {not (alive (VTS_Freecam_players select VTS_Freecam_players_select))} do {
				VTS_Freecam_players_select = VTS_Freecam_players_select + 1;
				if (VTS_Freecam_players_select > VTS_Freecam_players_count) then {VTS_Freecam_players_select = 0};
			};
			VTS_Freecam_target = vehicle (VTS_Freecam_players select VTS_Freecam_players_select);	
		};
		cutText [format[""%1"", name VTS_Freecam_target],""PLAIN""];
		if (VTS_Freecam_camera_on) then {
		VTS_Freecam_camera setpos [((position VTS_Freecam_target) select 0) + ((sin (getdir VTS_Freecam_target)) * ( - 10)), ((position VTS_Freecam_target) select 1) + ((cos (getdir VTS_Freecam_target)) * (- 10)), ((position VTS_Freecam_target) select 2) + 10];
		VTS_Freecam_camera setdir getdir VTS_Freecam_target;
		VTS_Freecam_camera camSetTarget VTS_Freecam_target;
		}
		else {VTS_Freecam_target switchCamera ""EXTERNAL"";};
		VTS_Freecam_camera_help = false;
	};
"];
*/
/*
_mousez = (findDisplay 46) displayAddEventHandler ["mousezchanged", "
	if ({alive _x} count VTS_Freecam_players > 0) then {
		_z = _this select 1;
		if (_z  < 0) then {
			VTS_Freecam_players_select = VTS_Freecam_players_select - 1;
			if (VTS_Freecam_players_select < 0) then {VTS_Freecam_players_select = VTS_Freecam_players_count};
			while {not (alive (VTS_Freecam_players select VTS_Freecam_players_select))} do {
				VTS_Freecam_players_select = VTS_Freecam_players_select - 1;
				if (VTS_Freecam_players_select < 0) then {VTS_Freecam_players_select = VTS_Freecam_players_count};
			};
			VTS_Freecam_target = vehicle (VTS_Freecam_players select VTS_Freecam_players_select);		
		}
		else {
			VTS_Freecam_players_select = VTS_Freecam_players_select + 1;
			if (VTS_Freecam_players_select > VTS_Freecam_players_count) then {VTS_Freecam_players_select = 0};
			while {not (alive (VTS_Freecam_players select VTS_Freecam_players_select))} do {
				VTS_Freecam_players_select = VTS_Freecam_players_select + 1;
				if (VTS_Freecam_players_select > VTS_Freecam_players_count) then {VTS_Freecam_players_select = 0};
			};
			VTS_Freecam_target = vehicle (VTS_Freecam_players select VTS_Freecam_players_select);	
		};
		cutText [format[""%1"", name VTS_Freecam_target],""PLAIN""];
		if (VTS_Freecam_camera_on) then {
		VTS_Freecam_camera setpos [((position VTS_Freecam_target) select 0) + ((sin (getdir VTS_Freecam_target)) * ( - 10)), ((position VTS_Freecam_target) select 1) + ((cos (getdir VTS_Freecam_target)) * (- 10)), ((position VTS_Freecam_target) select 2) + 10];
		VTS_Freecam_camera setdir getdir VTS_Freecam_target;
		VTS_Freecam_camera camSetTarget VTS_Freecam_target;
		}
		else {VTS_Freecam_target switchCamera ""EXTERNAL"";};
		VTS_Freecam_camera_help = false;
	};
"];
*/

//create the local uaz


//setpos loop 20 M before the camera

while {!stopcam} do {
sleep 0.01;
_Objpos = VTS_Freecam modelToWorld _Offset;
Obj setPos [_Objpos select 0,_Objpos select 1,Zpos];
};


//exit
//waitUntil {stopcam};
deleteVehicle Obj;
deleteVehicle object3D;
deleteVehicle ObjArrow;
VTS_Freecam_target switchCamera "INTERNAL";
VTS_Freecam CameraEffect ["Terminate","Back"];
CamDestroy VTS_Freecam;
(finddisplay 46) displayRemoveEventHandler ["keydown",_keydown_Help];
(finddisplay 46) displayRemoveEventHandler ["keydown",_keydown_NightVision];
(finddisplay 46) displayRemoveEventHandler ["keydown",_keydown_space];
(finddisplay 46) displayRemoveEventHandler ["keydown",_keydown_Backspace];
(finddisplay 46) displayRemoveEventHandler ["keydown",_keydown_c_key];
(finddisplay 46) displayRemoveEventHandler ["mousezchanged",_mousez];
(finddisplay 46) displayRemoveEventHandler ["MouseButtonDown",_keydown_Rclick];
(finddisplay 46) displayRemoveEventHandler ["MouseButtonDown",_keydown_Lclick];

